package com.lzxm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.axis.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lzxm.po.SxInfo;
import com.lzxm.po.SxInfoVo;
import com.lzxm.po.SxType;
import com.lzxm.po.User;
import com.lzxm.service.SxService;
import com.lzxm.constant.Constans;
import com.lzxm.constant.ServerResponse;

@Controller
public class SXController {

	@Autowired
	private SxService sxService;
	
	
	@RequestMapping("/typelist.do")
	@ResponseBody
	public ServerResponse getAllTypes(){
		ModelAndView mv = new ModelAndView();
		List<SxType> lists = sxService.selectAllType();
		ServerResponse response = ServerResponse.createBySuccessData(lists);
		return response;
	}
	
	
	@RequestMapping("/sx/add.do")
	@ResponseBody
	public ServerResponse insertSx(HttpServletRequest request){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		ServerResponse response;
		if(user == null){
			response = ServerResponse.createByErrorMsg("请先登陆！");
			return response;
		}
		
		String sx_name = request.getParameter("sx_name");
		int sx_type_id = Integer.parseInt(request.getParameter("sx_type_id"));
		String sx_content = request.getParameter("sx_content");
		String sx_enterprice = request.getParameter("sx_enterprice");
		
		if(StringUtils.isEmpty(sx_name) || StringUtils.isEmpty(sx_content)){
			response = ServerResponse.createByErrorMsg("字段不能为空");
			return response;
		}
		
		SxInfo sxInfo = new SxInfo();
		sxInfo.setSx_founder_id(user.getUser_id());
		sxInfo.setSx_name(sx_name);
		sxInfo.setSx_type_id(sx_type_id);
		sxInfo.setSx_content(sx_content);
		sxInfo.setSx_enterprice(sx_enterprice);
		sxService.insertSx(sxInfo);
		
		response = ServerResponse.createBySuccessMsg( "操作成功！");
		return response;
	}
	
	//删除事务
	@RequestMapping("/sx/del.do")
	@ResponseBody
	public ServerResponse deleteSx(HttpServletRequest request){
		ServerResponse response;
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user == null){
			response = ServerResponse.createByErrorMsg("请先登陆！");
			return response;
		}
		
		String id = request.getParameter("sx_id");
		if(StringUtils.isEmpty(id)){
			response = ServerResponse.createByErrorMsg("id不能为空！！");
			return response;
		}
		
		sxService.deleteSxById(Integer.parseInt(id));
		response = ServerResponse.createBySuccessMsg( "操作成功！");
		return response;
	}
	
	
		@RequestMapping("/sx/get.do")
		@ResponseBody
		public ServerResponse selectSx(HttpServletRequest request){
			ServerResponse response;
			HttpSession session = request.getSession();
			User user = (User) session.getAttribute("user");
			if(user == null){
				response = ServerResponse.createByErrorMsg("请先登陆！");
				return response;
			}
			List<SxInfoVo> list;
		try {
			String name = request.getParameter("sx_name");
			int status = Integer.parseInt(request.getParameter("status"));
			int type = Integer.parseInt(request.getParameter("type"));
			list = sxService.selectSxInfo(name);
		} catch (Exception e) {
			list = sxService.selectSxInfo(null);
		}
			
			response = ServerResponse.createBySuccessData(list);
			return response;
		}
	
	//注销事务
	@RequestMapping("/sx/login/out.do")
	@ResponseBody
	public ServerResponse outSx(HttpServletRequest request){
		ServerResponse response;
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user == null){
			response = ServerResponse.createByErrorMsg("请先登陆！");
			return response;
		}
		
		//todo 身份判断
		
		String id = request.getParameter("sx_id");
		if(StringUtils.isEmpty(id)){
			response = ServerResponse.createByErrorMsg("id不能为空！！");
			return response;
		}
		
		SxInfo sxInfo = new SxInfo();
		sxInfo.setSx_id(Integer.parseInt(id));
		sxInfo.setSx_status_id(Constans.LOGIN_OUT_SX);
		sxService.updateSx(sxInfo);
		response = ServerResponse.createBySuccessMsg( "操作成功！");
		return response;
	}
	
	//审批事务
	@RequestMapping("/sx/pass.do")
	@ResponseBody
	public ServerResponse passSx(HttpServletRequest request){
		ServerResponse response;
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user == null){
			response = ServerResponse.createByErrorMsg("请先登陆！");
			return response;
		}
		
		//todo 身份判断
		
		String id = request.getParameter("sx_id");
		String pass_id = request.getParameter("pass_id");
		if(StringUtils.isEmpty(id) || StringUtils.isEmpty(pass_id)){
			response = ServerResponse.createByErrorMsg("id不能为空！！");
			return response;
		}
		
		SxInfo sxInfo = new SxInfo();
		sxInfo.setSx_id(Integer.parseInt(id));
		
		if(Integer.parseInt(pass_id) == 0){
			sxInfo.setSx_status_id(Constans.PASS_OUT);
		}
		else{
			sxInfo.setSx_status_id(Constans.PASS);
		}
		
		sxService.updateSx(sxInfo);
		response = ServerResponse.createBySuccessMsg( "操作成功！");
		return response;
	}
	
	@RequestMapping("/sx/get/type.do")
	@ResponseBody
	public ServerResponse getSxByType(HttpServletRequest request){
		ServerResponse response;
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user == null){
			response = ServerResponse.createByErrorMsg("请先登陆！");
			return response;
		}
		
		String id = request.getParameter("sx_type_id");
		
		List<SxInfoVo> list = sxService.getByType(Integer.parseInt(id));
		response = ServerResponse.createBySuccessData(list);
		return response;
	}
}
